﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data.SqlClient;
using System.Collections;
using System.IO;

namespace TS_XY_Train_Repaire.Main_Base
{
    public partial class Sys_Setting_Station_Camera : DevExpress.XtraEditors.XtraForm
    {
        public Sys_Setting_Station_Camera()
        {
            InitializeComponent();
        }

        private void loadTree_Camera()   //摄像头目录
        {
            DataTable _dt = new DataTable();
            treeList_Camera.Nodes.Clear();
            treeList_Camera.Refresh();
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            ArrayList pList = new ArrayList();
            _ls = new LeverString_SysTreeMainList();
            _ls.f_id = "M-1";
            _ls.f_name = "摄像头目录";
            _ls.f_imgindex = 4;
            pList.Add(_ls);
            try
            {
                string strSQL = "select * from TS_S_B_Station_Camera where f_deep=0 and f_del=0 and f_type=0";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader read = cmd.ExecuteReader();
                string _strUPID = "and (";
                 int i = 0;
                while (read.Read())
                {
                    _ls = new LeverString_SysTreeMainList();
                    _ls.f_id = read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    _ls.f_parent = "M-1";
                    if (read["f_type"].ToString() == "0")
                    {
                        _ls.f_imgindex = 5;
                    }
                    else
                    {
                        _ls.f_imgindex = 9;
                    }
                    if (i == 0)
                    {
                        _strUPID += "(f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    else
                    {
                        _strUPID += "or (f_level like '%," + read["f_id"].ToString() + ",%')";
                    }
                    pList.Add(_ls);
                    i++;
                }
                _strUPID += ")";
                if (_strUPID.Length > 7)
                {
                    read.Close();
                    strSQL = "select * from TS_S_B_Station_Camera where f_deep>0 and f_del=0 and f_type=0 " + _strUPID + " order by f_deep,f_level,f_name asc";
                    cmd = new SqlCommand(strSQL, conn);
                    read = cmd.ExecuteReader();
                    string _UPID = "";
                    while (read.Read())
                    {
                        _ls = new LeverString_SysTreeMainList();
                        _ls.f_id = read["f_id"].ToString();
                        _ls.f_name = read["f_name"].ToString();
                        _ls.f_deep = read["f_deep"].ToString();
                        _ls.f_level = read["f_level"].ToString();
                        _ls.f_type = read["f_type"].ToString();
                        _UPID = read["f_level"].ToString();
                        _UPID = _UPID.Substring(0, _UPID.Length - 1);
                        _UPID = _UPID.Substring(_UPID.LastIndexOf(',') + 1);
                        _ls.f_parent = _UPID;
                        if (read["f_type"].ToString() == "0")
                        {
                            _ls.f_imgindex = 5;
                        }
                        else
                        {
                            _ls.f_imgindex = 9;
                        }
                        pList.Add(_ls);
                    }
                }
                read.Close();
                conn.Close();
                conn.Dispose();
                }
            catch 
            {
               
        
            }
            treeList_Camera.DataSource = pList;   
        }

        private void girdList(string strA)
        {
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable("select * from TS_S_B_Station_Camera where f_del=0 and f_type=1 " + strA + "", dt);
            CameraGird.DataSource = dt;
        }

        private void girdList()
        {
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable("select * from TS_S_B_Station_Camera where f_del=0 and f_type=1 ",dt);
            CameraGird.DataSource = dt;
        }

        private void Sys_Setting_Station_Camera_Load(object sender, EventArgs e)
        {
            bbi_Add.Enabled = true;
            bbi_Edit.Enabled = false;
            bbi_Del.Enabled = false;
            loadTree_Camera();
            //girdList();
        }

        private void treeList_Camera_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            try
            {
                if (treeList_Camera.FocusedNode["f_id"].ToString().Substring(0, 1) == "M")
                {
                    bbi_Add.Enabled = true;
                    bbi_Edit.Enabled = false;
                    bbi_Del.Enabled = false;
                    // girdList();
                }
                else
                {
                    bbi_Add.Enabled = true;
                    bbi_Edit.Enabled = true;
                    bbi_Del.Enabled = true;

                    // girdList("  and f_level like '" + treeList_Camera.FocusedNode["f_level"].ToString() + "" + treeList_Camera.FocusedNode["f_id"].ToString() + ",%'");
                }
            }
            catch 
            {
                

            }

         }

        private void CameraGird_Click(object sender, EventArgs e)
        {
            bbi_Add.Enabled = false;
            bbi_Del.Enabled = true;
            bbi_Edit.Enabled = true;
        }

        private void bbi_Reload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            loadTree_Camera();
            CameraGird.DataSource=null;
        }

        private void bbi_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            _ls.f_id = treeList_Camera.FocusedNode["f_id"].ToString();
            _ls.f_name = treeList_Camera.FocusedNode["f_name"].ToString();
            _ls.f_deep = treeList_Camera.FocusedNode["f_deep"].ToString();
            _ls.f_level = treeList_Camera.FocusedNode["f_level"].ToString();
            _ls.f_type = treeList_Camera.FocusedNode["f_type"].ToString();
            Sys_Setting_Station_CameraEdit _uc = new Sys_Setting_Station_CameraEdit();
            _uc._tl = _ls;
            _uc._LoadType = "Add";

            if (_uc.ShowDialog() == DialogResult.OK)
            {
                loadTree_Camera();
                treeList_Camera.SetFocusedNode(treeList_Camera.FindNodeByFieldValue("f_id", _ls.f_id));
                girdList();
            }
            else
            {
            }
        }

        private void bbi_Del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            _ls.f_id = treeList_Camera.FocusedNode["f_id"].ToString();
            _ls.f_name = treeList_Camera.FocusedNode["f_name"].ToString();
            _ls.f_deep = treeList_Camera.FocusedNode["f_deep"].ToString();
            _ls.f_level = treeList_Camera.FocusedNode["f_level"].ToString();
            _ls.f_type = treeList_Camera.FocusedNode["f_type"].ToString();
            Sys_Setting_Station_CameraEdit _uc = new Sys_Setting_Station_CameraEdit();
            _uc._tl = _ls;
            _uc._LoadType = "Del";
            //girdValue();
            _uc._name = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_name").ToString();
            _uc._ipl = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_ip_l").ToString();
            _uc._ipw = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_ip_w").ToString();
            _uc._user = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_logname").ToString();
            _uc._pass = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_logpass").ToString();
            _uc._port = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_port").ToString();
            _uc._nvr_ipl = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_ip_l").ToString();
            _uc._nvr_ipw = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_ip_w").ToString();
            _uc._nvr_user = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_logname").ToString();
            _uc._nvr_pass = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_logpass").ToString();
            _uc._nvr_port = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_port").ToString();
            _uc._nvr_num = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_num").ToString();
            _uc._id = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_id").ToString();
            if (CameraGird.Focused==true)
            {
                _uc._Select = "1"; 
            }
  
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                
                 loadTree_Camera();
                treeList_Camera.SetFocusedNode(treeList_Camera.FindNodeByFieldValue("f_id", _ls.f_id));
                girdList();
            }
            else
            {
            }  
        }

        private void bbi_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            _ls.f_id = treeList_Camera.FocusedNode["f_id"].ToString();
            _ls.f_name = treeList_Camera.FocusedNode["f_name"].ToString();
            _ls.f_deep = treeList_Camera.FocusedNode["f_deep"].ToString();
            _ls.f_level = treeList_Camera.FocusedNode["f_level"].ToString();
            _ls.f_type = treeList_Camera.FocusedNode["f_type"].ToString();
            Sys_Setting_Station_CameraEdit _uc = new Sys_Setting_Station_CameraEdit();
            _uc._tl = _ls;
            _uc._LoadType = "Edit";
            _uc._name = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_name").ToString();
            _uc._ipl = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_ip_l").ToString();
            _uc._ipw = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_ip_w").ToString();
            _uc._user = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_logname").ToString();
            _uc._pass = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_logpass").ToString();
            _uc._port = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_port").ToString();
            _uc._nvr_ipl = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_ip_l").ToString();
            _uc._nvr_ipw = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_ip_w").ToString();
            _uc._nvr_user = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_logname").ToString();
            _uc._nvr_pass = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_logpass").ToString();
            _uc._nvr_port = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_port").ToString();
            _uc._nvr_num = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_nvr_num").ToString();
            _uc._id = CamGridView.GetRowCellDisplayText(CamGridView.FocusedRowHandle, "f_id").ToString();
            if (CameraGird.Focused == true)
            {
                _uc._Select = "1";
            }
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                loadTree_Camera();
                treeList_Camera.SetFocusedNode(treeList_Camera.FindNodeByFieldValue("f_id", _ls.f_id));
                girdList();
            }
            else
            {
            } 
        }

        private void treeList_Camera_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (treeList_Camera.FocusedNode["f_id"].ToString().Substring(0, 1) == "M")
                {
                    girdList();
                }
                else
                {
                    girdList("  and f_level like '" + treeList_Camera.FocusedNode["f_level"].ToString() + "" + treeList_Camera.FocusedNode["f_id"].ToString() + ",%'");
                }
            }
            catch
            {
            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Directory.Exists(TS_Win_Class._EXP_Direct) == false)
            {
                Directory.CreateDirectory(TS_Win_Class._EXP_Direct);
            }  
            if (MessageBox.Show("文件导出为" + TS_Win_Class._EXP_Direct + System.DateTime.Today.Year.ToString() + "-" + System.DateTime.Today.Month.ToString() + "-" + System.DateTime.Today.Day.ToString() + "摄像头导出列表.xls", "注意", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                CameraGird.ExportToXls(TS_Win_Class._EXP_Direct + System.DateTime.Today.Year.ToString() + "-" + System.DateTime.Today.Month.ToString() + "-" + System.DateTime.Today.Day.ToString() + "摄像头列表.xls");
            }
        }

        private void Sys_Setting_Station_Camera_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }


    }
}